package com.pb.infra.auth.controller;

import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.auth.entity.pbsys.PriviPost;
import com.pb.infra.auth.service.PriviPostService;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
 * 岗位管理
 *
 * @author liudebao
 * @date 2020/12/25
 */
@Slf4j
@RestController
@RequestMapping("v1/system/post")
public class PriviPostController {

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "PRIVI_POST";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "岗位管理";

    /**
     * 业务逻辑对象
     */
    @Resource
    private PriviPostService priviPostService;

    /**
     * 查询岗位信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(@RequestParam Map<String, Object> paramMap, Page page, HttpServletRequest request) {
        return priviPostService.select(paramMap,page, request);
    }


    /**
     * 新建岗位信息
     *
     * @param priviPost 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(PriviPost priviPost, HttpServletRequest request) {
        priviPostService.insert(priviPost, request);
        return Result.OK_EMPTY;
    }


    /**
     * 修改岗位信息
     *
     * @param priviPost 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(PriviPost priviPost, HttpServletRequest request) {
        priviPostService.update(priviPost, request);
        return Result.OK_EMPTY;
    }

    /**
     * 删除岗位信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<String> paramMap) {
        if (priviPostService.delete(paramMap)) {
            return Result.error("-1", "某用户的职位包含选中的岗位");
        } else {
            return Result.OK_EMPTY;
        }
    }

    /**
     * 恢复岗位
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("updateFlag")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result updateFlag(@RequestBody List<String> paramMap) {
        priviPostService.updateFlag(paramMap);
        return Result.OK_EMPTY;
    }

}
